package 数组.easy;

import java.util.Arrays;

/**
 * @author Lw中
 * @date 2020/9/13 9:09
 */
public class ErQi {

    /**
     * 原地移除元素
     * @param nums
     * @param val
     * @return
     */
    public static int removeElement(int[] nums, int val) {
        // low指针只有在val和nums[i]不相等时才移动
        int low = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val) {
                nums[low] = nums[i];
                low+=1;
            }
        }
        for (int i = 0; i < low; i++) {
            if (i != low - 1) {
                System.out.print(nums[i] + " ");
            } else {
                System.out.println(nums[i]);
            }
        }
        return low;
    }

    public static void main(String[] args) {
        int[] nums = {0,1,2,2,3,0,4,2};
        int i = removeElement(nums, 2);
        System.out.println(i);
    }

}
